#ifndef WL_EXT_FS_H
#define WL_EXT_FS_H

typedef void* wl_ext_fs_handle;

#define WL_EXT_INVALID_FS NULL

enum {
    EX_SEEK_SET, 
    EX_SEEK_CUR, 
    EX_SEEK_END
};

#define WL_EXT_RDONLY 0x001
#define WL_EXT_WRONLY 0x002
#define WL_EXT_RDWR (WL_EXT_RDONLY | WL_EXT_WRONLY)
#define WL_EXT_CREAT 0x004
#define WL_EXT_APPEND 0x008

/*
    此文件为文件系统接口，需要实现相关操作功能，包括
    open, close, read, write, delete, seek, file size, file exists
    调用者使用多个路径是相对./的路径，所以相应的在平台实现接口的时候，需要
    把此相对接口映射到实际的绝对路径。
*/

/**
 * open file by path
 * 
 * @param path file path
 * @param flags open flags, WL_RDONLY, WL_WRONLY, WL_RDWR, WL_CREAT, WL_APPEND
 * @return file handle success for non-null, fail for null
 */
wl_ext_fs_handle wl_ext_fs_open(const char* path, int flags);

/**
 * close file by handle
 * 
 * @param handle file handle
 * @return 0 success, -1 fail
 */
int wl_ext_fs_close(wl_ext_fs_handle handle);

/**
 * read file by handle
 * 
 * @param handle file handle
 * @param buf read buffer
 * @param len bytes to read
 * @return bytes read, or < 0 for error
 */
int wl_ext_fs_read(wl_ext_fs_handle handle, void* buf, int len);

/**
 * write file by handle
 * 
 * @param handle file handle
 * @param buf write buffer
 * @param len bytes to write    
 * @return bytes written, or < 0 for error
 */
int wl_ext_fs_write(wl_ext_fs_handle handle, const void* buf, int len);

/**
 * 删除指定路径的文件，调用者传入的文件是相对路径，实现者需要根据平台，填充完整的路径
 * 
 * @param path file path
 * @return 0 success, -1 fail
 */
int wl_ext_fs_delete(const char* path);

/**
 * seek file by handle
 * 
 * @param handle file handle
 * @param offset seek offset
 * @param whence EX_SEEK_SET, EX_SEEK_CUR, EX_SEEK_END
 * 
 * @return 0 success, -1 fail
 */
int wl_ext_fs_seek(wl_ext_fs_handle handle, int offset, int whence);

/**
 * 获取指定路径的文件大小，调用者传入的文件是相对路径，实现者需要根据平台，填充完整的路径
 * 
 * @param path file path
 * @return file size, or < 0 for error
 */
int wl_ext_fs_size_by_path(const char* path);

/**
 * get file size by handle
 * 
 * @param handle file handle
 * @return file size, or < 0 for error
 */
int wl_ext_fs_size_by_handle(wl_ext_fs_handle handle);

/**
 * 判断指定路径的文件是否存在，调用者传入的文件是相对路径，实现者需要根据平台，填充完整的路径
 * 
 * @param path file path
 * @return 0 not exists, 1 exists
 */
int wl_ext_fs_exists(const char* path);

/**
 * 重命名文件名，调用者传入的文件是相对路径，实现者需要根据平台，填充完整的路径
 * 
 * @param src source path
 * @param dst destination path
 * @return 0 success, -1 fail
 */
int wl_ext_fs_rename(const char* src, const char* dst);

#endif